//
//  CZQUserViewController.m
//  Project_A
//
//  Created by dlios on 15-7-13.
//  Copyright (c) 2015年 zhoukaidong. All rights reserved.
//

#import "CZQUserViewController.h"
#import "UMSocial.h"
#import "UIImageView+WebCache.h"
#import "BaseHeader.h"
#import "UIButton+WebCache.h"
#import "XiaoKaiUserCollectionView.h"
#import "XiaoKaiUserTableView.h"
#import "XiaoKaiUserTableView.h"
#import "XiaoKaiUserCollectionView.h"
#import "XiaoKaiUserFirstCollectionView.h"
#import "XiaoKaiUserSecondCollectionView.h"
#import "XiaoKaiUserConfigViewController.h"
#import "DataBase.h"
#import "XiaoKaiUserCollectionViewCell.h"
#import "XiaoKaiUserNewTableViewCell.h"
#import "XiaoKaiRaidersViewController.h"
@interface CZQUserViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic , retain)UILabel *label;

@property (nonatomic , retain)UIImageView *image;

@property (nonatomic , retain)UIButton *defaultbutton;

@property (nonatomic , retain)UIButton *Loginbutton;

@property (nonatomic , retain)UILabel *movelabel;

@property (nonatomic , retain)UIButton *leftbutton;

@property (nonatomic , retain)UIButton *righebutton;

@property (nonatomic , retain)UITableView *UserTableView;

@property (nonatomic , retain)UIView *Myview;

@property (nonatomic, retain) UIView *nologinMyview;

@property (nonatomic, retain) NSArray *dataBaseArr;

@property (nonatomic , retain)UIImageView *ShareView;


@property (nonatomic , retain)UILabel *ShareLabel;

@end

@implementation CZQUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    NSDictionary *snsAccountDic = [UMSocialAccountManager socialAccountDictionary];
    UMSocialAccountEntity *sinaAccount = [snsAccountDic valueForKey:UMShareToSina];
    self.Myview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3 + SCREEN_HEIGHT / 13 + 5)];
    if (sinaAccount.userName != nil) {
        [self getData];
        [self creatNav];
        [self loginedcreatview];
        [self creatCollectionView];
        [self getbutton];
    } else {
        
        [self creatNav];
        [self NotLoginCreatView];
        [self creatCollectionView];
        [self getbutton];
        [self.view bringSubviewToFront:self.ShareView];
        self.UserTableView.scrollEnabled = NO;
    }

}



//判断创建已经登录的视图
-(void)loginedcreatview
{
    NSDictionary *snsAccountDic = [UMSocialAccountManager socialAccountDictionary];
    UMSocialAccountEntity *sinaAccount = [snsAccountDic valueForKey:UMShareToSina];
    self.image = [[UIImageView  alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3 + SCREEN_HEIGHT / 13 + 5)];
    self.image.backgroundColor = [UIColor clearColor];
    self.image.image = [UIImage imageNamed:@"2.jpg"];
    self.image.userInteractionEnabled = YES;
    
    self.Loginbutton = [[UIButton alloc]initWithFrame:CGRectMake( self.image.frame.size.width / 2 - 60, self.image.frame.size.height / 2 - 50, 100, 100)];
    
    [self.Loginbutton.layer setCornerRadius:10];
    self.Loginbutton.backgroundColor = [UIColor clearColor];
    [self.Loginbutton setBackgroundImageWithURL:[NSURL URLWithString:sinaAccount.iconURL] forState:UIControlStateNormal];
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT / 3 -  55, SCREEN_WIDTH, 50)];
    self.label.text = sinaAccount.userName;
    self.label.textColor = [UIColor whiteColor];
    self.label.font = [UIFont systemFontOfSize:20];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.backgroundColor = [UIColor clearColor];
    
    self.defaultbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.defaultbutton.frame = CGRectMake(SCREEN_WIDTH - 50, 30, 50, 32);
    [self.defaultbutton setTitle:@"注销" forState:UIControlStateNormal];
    self.defaultbutton.titleLabel.font = [UIFont systemFontOfSize:13];
    self.defaultbutton.backgroundColor = [UIColor clearColor];
    
    self.ShareLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 3, SCREEN_WIDTH, SCREEN_HEIGHT / 13 + 5)];
    self.ShareLabel.backgroundColor = [UIColor clearColor];
    self.ShareLabel.text = @"喜欢的收藏";
    self.ShareLabel.font = [UIFont systemFontOfSize:21];
//    self.ShareLabel.layer.borderWidth = 1;
//    self.ShareLabel.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    [self.ShareLabel.layer setCornerRadius:10];
    self.ShareLabel.clipsToBounds = YES;
    self.ShareLabel.textAlignment = NSTextAlignmentCenter;
    self.ShareLabel.textColor = [UIColor whiteColor];
    self.ShareLabel.backgroundColor = [UIColor clearColor];
    [self.image addSubview:self.ShareLabel];
    self.ShareLabel.backgroundColor = RGBACOLOR(230, 230, 230, 0.1);
//    self.ShareLabel.alpha = 0.6;
    
    [self.defaultbutton addTarget:self action:@selector(zhuxiao) forControlEvents:UIControlEventTouchUpInside];
    [self.image addSubview:self.label];
    [self.Myview addSubview:self.image];
    [self.image addSubview:self.Loginbutton];
    [self.Myview addSubview:self.defaultbutton];
    [self.Myview bringSubviewToFront:self.defaultbutton];
      self.UserTableView.bounces = NO;
    

    
}
-(void)getbutton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(SCREEN_WIDTH - 50, 28, 32, 32);
    button.backgroundColor = [UIColor orangeColor];
    [button addTarget:self action:@selector(shezhi) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    [self.view bringSubviewToFront:button];
}
-(void)shezhi
{
    XiaoKaiUserConfigViewController *Config = [[XiaoKaiUserConfigViewController alloc]init];
    Config.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:Config animated:YES];
}
-(void)NotLoginCreatView
{
    NSDictionary *snsAccountDic = [UMSocialAccountManager socialAccountDictionary];
    UMSocialAccountEntity *sinaAccount = [snsAccountDic valueForKey:UMShareToSina];
    self.image = [[UIImageView  alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3 + SCREEN_HEIGHT / 13 + 5)];
    self.image.userInteractionEnabled = YES;
    self.image.image = [UIImage imageNamed:@"2.jpg"];
    self.image.backgroundColor = [UIColor clearColor];
    
    self.Loginbutton = [[UIButton alloc]initWithFrame:CGRectMake( self.image.frame.size.width / 2 - 50, self.image.frame.size.height / 2 - 50, 100, 100)];
    [self.Loginbutton.layer setCornerRadius:10];
    self.Loginbutton.layer.masksToBounds = YES;
    self.Loginbutton.backgroundColor = [UIColor clearColor];
    [self.Loginbutton setBackgroundImageWithURL:[NSURL URLWithString:sinaAccount.iconURL] forState:UIControlStateNormal];
    [self.Loginbutton setTitle:@"点击登录" forState:UIControlStateNormal];
    self.Loginbutton.tintColor = [UIColor whiteColor];
    
    self.ShareLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 3, SCREEN_WIDTH, SCREEN_HEIGHT / 13 + 5)];
    self.ShareLabel.backgroundColor = [UIColor clearColor];
    self.ShareLabel.text = @"喜欢的收藏";
    self.ShareLabel.font = [UIFont systemFontOfSize:21];
    [self.ShareLabel.layer setCornerRadius:10];
    self.ShareLabel.clipsToBounds = YES;
//    self.ShareLabel.layer.borderWidth = 1;
//    self.ShareLabel.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    self.ShareLabel.textAlignment = NSTextAlignmentCenter;
    self.ShareLabel.textColor = [UIColor whiteColor];
    self.ShareLabel.backgroundColor = RGBACOLOR(230, 230, 230, 0.1);
//    self.ShareLabel.alpha = 0.8;
    [self.image addSubview:self.ShareLabel];

    
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 3 - 55, SCREEN_WIDTH, 50)];
    self.label.backgroundColor = [UIColor clearColor];
    
    [self.Loginbutton addTarget:self action:@selector(denglu) forControlEvents:UIControlEventTouchUpInside];
    [self.view bringSubviewToFront:self.defaultbutton];
    
    [self.image addSubview:self.label];
    [self.Myview addSubview:self.image];
    [self.image addSubview:self.Loginbutton];
    self.ShareView = [[UIImageView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 3 + SCREEN_HEIGHT / 6 + 5, SCREEN_WIDTH, (SCREEN_HEIGHT - SCREEN_HEIGHT / 3 + SCREEN_HEIGHT / 13 + 5 - 49 - 64))];
    self.ShareView.backgroundColor = [UIColor clearColor];
    self.ShareView.image = [UIImage imageNamed:@"zkddzwt_meitu_4"];
    [self.view addSubview:self.ShareView];
      self.UserTableView.bounces = NO;
}


-(void)denglu
{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response)
                                  {
                                      
                                      
                                      [self informetion];
                                      
                                  });
    
    
}


- (void)informetion
{
    NSDictionary *snsAccountDic = [UMSocialAccountManager socialAccountDictionary];
    UMSocialAccountEntity *sinaAccount = [snsAccountDic valueForKey:UMShareToSina];
    if (sinaAccount.userName != nil) {
        [self.Loginbutton setBackgroundImageWithURL:[NSURL URLWithString:sinaAccount.iconURL] forState:UIControlStateNormal];
        [self.Loginbutton setTitle:nil forState:UIControlStateNormal];
        self.label.frame = CGRectMake(0, 20 , SCREEN_WIDTH, 50);
        self.label.text = sinaAccount.userName;
        
        self.Loginbutton.userInteractionEnabled = NO;
        [self.Loginbutton.layer setCornerRadius:10];
        self.Loginbutton.layer.masksToBounds = YES;
        
        self.defaultbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.defaultbutton.frame = CGRectMake(SCREEN_WIDTH - 50, 30, 50, 32);
        [self.defaultbutton setTitle:@"注销" forState:UIControlStateNormal];
        self.defaultbutton.titleLabel.font = [UIFont systemFontOfSize:13];
        self.defaultbutton.backgroundColor = [UIColor clearColor];
        [self.defaultbutton addTarget:self action:@selector(zhuxiao) forControlEvents:UIControlEventTouchUpInside];
        self.label.textColor = [UIColor whiteColor];
        [self.Myview addSubview:self.defaultbutton];
//        self.defaultbutton.backgroundColor = NAVCOLOR;
        [self.Myview bringSubviewToFront:self.defaultbutton];
        
        [self.Loginbutton setBackgroundImageWithURL:[NSURL URLWithString:sinaAccount.iconURL] forState:UIControlStateNormal];
        self.UserTableView.scrollEnabled = YES;
        self.ShareView.hidden = YES;
        self.UserTableView.bounces = NO;
    }
}


-(void)creatNav
{
    UIView *naview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20 )];
    naview.backgroundColor = NAVCOLOR;
    [self.view addSubview:naview];
    UILabel *Navlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    Navlabel.backgroundColor = NAVCOLOR;
    Navlabel.text = @"我的";
    Navlabel.font = [UIFont systemFontOfSize:22];
    Navlabel.textColor = [UIColor whiteColor];
    Navlabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:Navlabel];
    [self.view bringSubviewToFront:Navlabel];
    
}


//实现回调方法
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    if (response.viewControllerType == UMSViewControllerOauth) {
        
    }
}

-(void)zhuxiao
{
    
    [[UMSocialDataService defaultDataService] requestUnBindToSnsWithCompletion:nil];
    [[UMSocialDataService defaultDataService] requestUnOauthWithType:UMShareToSina  completion:^(UMSocialResponseEntity *response){
    }];
    [self.Loginbutton setBackgroundImage:nil forState:UIControlStateNormal ];
    self.label.text = @"";
    [self.Loginbutton setTitle:@"点击登录" forState:UIControlStateNormal];
    self.Loginbutton.userInteractionEnabled = YES;
    self.image.userInteractionEnabled = YES;
    [self.Loginbutton addTarget:self action:@selector(denglu) forControlEvents:UIControlEventTouchUpInside];
    [self.defaultbutton removeFromSuperview];
    if (!self.ShareView) {
        self.ShareView = [[UIImageView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 3 + SCREEN_HEIGHT / 6 + 5, SCREEN_WIDTH, (SCREEN_HEIGHT - SCREEN_HEIGHT / 3 + SCREEN_HEIGHT / 13 + 5 - 49 - 64))];
        self.ShareView.backgroundColor = [UIColor clearColor];
         self.ShareView.image = [UIImage imageNamed:@"zkddzwt_meitu_4"];
        [self.view addSubview:self.ShareView];
    }
    self.UserTableView.scrollEnabled = NO;
    [self.view bringSubviewToFront:self.ShareView];
    self.ShareView.hidden = NO;
    
}

-(void)creatCollectionView
{
    self.UserTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:UITableViewStylePlain];
    self.UserTableView.rowHeight = SCREEN_HEIGHT / 4;
    self.UserTableView.delegate = self;
    self.UserTableView.dataSource = self;
    self.UserTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.UserTableView];
    self.UserTableView.tableHeaderView = _Myview;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XiaoKaiUserNewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableviewcell"];
    if (!cell) {
        cell = [[[XiaoKaiUserNewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableviewcell"] autorelease];
    }
    cell.Model = self.dataBaseArr[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataBaseArr.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"你瞅啥");
    XiaoKaiRaidersViewController *xiaokai = [[[XiaoKaiRaidersViewController alloc]init]autorelease];
    UserModel *model = [self.dataBaseArr objectAtIndex:indexPath.row];
    xiaokai.webviewrequest = model.URL;
    xiaokai.labeltext = model.name;
    xiaokai.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:xiaokai animated:YES];
    
}

- (void)getData
{
    DataBase *dbManger = [DataBase shareInstance];
    [dbManger openDB];
    self.dataBaseArr = [dbManger selectInfo];
    [dbManger closeDB];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self informetion];
    [self getData];
    [self.UserTableView reloadData];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
